package com.echo.fm.testexcel;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.echo.fm.content.activity.IActionService;
import com.echo.fm.content.activity.model.Action;
import com.echo.util.ErrorCode;
import com.echo.util.ErrorException;
import com.echo.util.ResultUtils;
import com.echo.util.impl.ExcelExportUtil;
@Controller
public class ITestExcelController {
	@Autowired
	@Qualifier("actionService")
	IActionService actionService;
	
	@RequestMapping(value = "/web/test/excel")
	@ResponseBody
	Object testExcel(HttpServletResponse response, HttpServletRequest request) {

		List<String> headList = new ArrayList<String>();
		headList.add("活动ID");
		headList.add("活动标题");
		headList.add("活动介绍");

		try {
			
			List<Action> rowdata = actionService.searchActionList();
					//.searchALLActionList();
			List<List> datalist = new ArrayList<List>();
			for (Action action : rowdata) {
				datalist.add(transAction2List(action));
			}
			ExcelExportUtil excelService = new ExcelExportUtil();
			String filename = "action";
			excelService.exportExcel(response, filename, headList, datalist);
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);

		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_FAILED_CODE);
		}
	}

	private List transAction2List(Action action) throws ErrorException {
		List rowList = new ArrayList();

		try {

			rowList.add((Integer) action.getActionId());
			rowList.add((String) action.getTitle());
			rowList.add((String) action.getIntro());
			return rowList;
		} catch (Exception ex) {
			throw new ErrorException(ErrorCode.INTERNAL_SERVER_ERROR);
		}
	}

}
